﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 共享单车管理系统
{
    public class Cartltemsv
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        public shop Add(int userId, int dancheId, int Qty)
        {
            shop cartItem = null;
            Bike bike = (from p in db.Bike
                         where p.BikeID == dancheId
                         select p).First();
            cartItem = new shop();

            cartItem.userid = userId;
            cartItem.BikeID = bike.BikeID;
            cartItem.dancheleixing = bike.BikeType;
            cartItem.jiage = bike.Price.Value;
            cartItem.chengbenjiage=bike.chengbenjiages.Value;
            cartItem.qty = Qty;
            int ExistCartItemCount = (from c in db.shop
                                      where c.userid == userId
                                      && c.BikeID == dancheId
                                      select c).Count();

            if (ExistCartItemCount > 0)  //修改
            {
                shop existCartItem = (from c in db.shop
                                          where c.userid == userId
                                          && c.BikeID == dancheId
                                          select c).First();
                existCartItem.qty += Qty;  //添加
            }
            else
            {
                db.shop.InsertOnSubmit(cartItem);
            }

            db.SubmitChanges();
            return cartItem;

        }

        public (decimal, decimal) GetTotalPriceByUserID(int userID)
        {
            List<shop> list = (from c in db.shop
                                   where c.userid == userID
                                   select c).ToList();
            return ((decimal)list.Sum(c => c.jiage * c.qty), (decimal)list.Sum(c => (c.jiage - c.chengbenjiage)));
        }
    }

}

